package com.hedl.practice.diyJava.methodAndStructure;

import java.util.Arrays;

public class StudyMethod {

    public static void main(String[] args) {
        /*Person p = new Person("lisi",20);
        *//*p.setName("lisi");
        p.setAge(20);*//*
        System.out.println(p.getName()+","+p.getAge());
        System.out.println(p);*/

        //可变方法参数练习
        /*Group g = new Group("Xiao Ming","Xiao Li","Xiao Jun");
        System.out.println(g);*/

        //另一个练习方式
        /*Group g = new Group();
        g.setName(new String[]{"Xiao Ming","Xiao Li","Xiao Jun"});
        System.out.println(g);
        System.out.println("尝试使用toString:"+g.toString());
        System.out.println("数组长度："+g.getNames().length);
        System.out.println("数组长度,toString："+g.toString().length());*/

        //参数绑定测试
        /**
         * 修改外部的局部变量n，不影响实例p的age字段，原因是setAge()方法获得的参数，复制了n的值，因此，p.age和局部变量n互不影响。
         * 结论：基本类型参数的传递，是调用方值的复制。双方各自的后续修改，互不影响。
         */
        /*Person02 p2 = new Person02();
        int n = 15;
        p2.setAge(n);
        System.out.println("age值："+p2.getAge());
        n = 20;
        System.out.println("age值："+p2.getAge());*/

        /**
         * 传递引用参数
         *  结论：引用类型参数的传递，调用方的变量，和接收方的参数变量，指向的是同一个对象。双方任意一方对这个对象的修改，都会影响对方（因为指向同一个对象嘛）。
         */

        Person03 p3 = new Person03();
        String[] fullname = new String[]{"Homer","Simpson"};
        p3.setName(fullname);
        System.out.println("p3查看值："+p3.getName());
        fullname[0] = "Bart";
        System.out.println("p3查看值："+p3.getName());
    }
}

class Person{
    private String name;
    private Integer age;

    public String getName(){
        return "姓名："+name;
    }

    public Integer getAge(){
        return age;
    }

    /*public void setName(String name){
        this.name = name;
    }

    public void setAge(int age){
        this.age = age;
    }*/

    public Person(String name,Integer age){
        this.name = name;
        this.age = age;
    }

    //只能在类中访问，反射注入例外；
    private Integer calcAge(Integer currentYear){
        return currentYear-this.age;
    }

    public Person(){
    }

    public String toString(){
        return "姓名："+name+"，年龄："+age;
    }
}

//可变方法参数练习
class Group{
    private String[] names;


    //加上个"..."表示可变参数个数，适合数组类型
    /*public void setNames(String ... names){
        this.names = names;
    }*/

    //另外一个写法
    public void setName(String[] names){
        this.names = names;
    }

    public String[] getNames(){
        return names;
    }

    public Group(){}

    /*public Group(String ... names){
        this.names = names;
    }*/

    public String toString(){
        return "["+ Arrays.toString(names)+"]";
    }

}

//参数绑定
class Person02{
    private Integer age;

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}

//引用参数传递
class Person03{
    private String[] name;

    public String getName(){
        return this.name[0]+","+this.name[1];
    }

    public void setName(String[] name){
        this.name = name;
    }
}
